#include "qimage.h"

QNamespaceStart(Qui)

void QPutimage(int x, int y, QImage* image_beput,
		int transparency, IMAGE* target)
{
	IMAGE temp = image_beput->get_image();

	HDC target_dc = GetImageHDC(target);
	HDC image_dc = GetImageHDC(&temp);

	int width = image_beput->get_width();
	int height = image_beput->get_height();

	BLENDFUNCTION blend_function = { AC_SRC_OVER, 0, static_cast<BYTE>(transparency), AC_SRC_ALPHA };
	AlphaBlend(target_dc, x, y, width, height,
		image_dc, 0, 0, width, height,
		blend_function);
}

void QImage::cut_image(QImageCutReact cut_react, int width, int height)
{
	using namespace Gdiplus;

	IMAGE new_image = IMAGE(width, height);

	HDC memory_dc = CreateCompatibleDC(QGetImageHDC(&save_image));
	HBITMAP bit_map = CreateCompatibleBitmap(QGetImageHDC(&save_image), save_image.getwidth(), save_image.getheight());
	HBITMAP old_bit_map = (HBITMAP)SelectObject(memory_dc, bit_map);

	BitBlt(memory_dc, 0, 0, width, height, QGetImageHDC(&save_image), 0, 0, SRCCOPY);

	bit_map = (HBITMAP)SelectObject(memory_dc, old_bit_map);

	DeleteDC(memory_dc);

	Bitmap convert_image(bit_map, NULL);
	Graphics     draw_graphics(GetImageHDC(&new_image));
	GraphicsPath draw_path;

	TextureBrush brush(&convert_image);

	switch (cut_react)
	{
	case ImageCutEllipse:
	{
		QShapeSetHighQuality(draw_graphics);

		draw_graphics.FillEllipse(&brush, Rect{ 0, 0, width, height });

		save_image = new_image;

		break;
	}
	case ImageCutRectangle:
	{
		QShapeSetHighQuality(draw_graphics);

		draw_graphics.FillRectangle(&brush, Rect{ 0, 0, width, height });

		save_image = new_image;

		break;
	}
	}
}

void QImage::cut_image_with_shadow(QImageCutReact cut_react, int width,
	int height, int offset_pixel)
{
	using namespace Gdiplus;

	cut_image(cut_react, width, height);

	IMAGE temp_image = save_image;
	save_image = IMAGE(width + offset_pixel, height + offset_pixel);

	QPutimage(0, 0, &temp_image, 255, &save_image);

	Graphics graphics(QGetImageHDC(&save_image));
	GraphicsPath draw_path;

	if (cut_react == ImageCutRectangle)
	{
		draw_path.AddRectangle(Rect{ 0, 0, width, height });
	}
	if (cut_react == ImageCutEllipse)
	{
		draw_path.AddEllipse(Rect{ 0, 0, width, height });
	}

	graphics.SetPageUnit(UnitPixel);

	GraphicsPath& shadow_path = *(draw_path.Clone());

	Matrix shadow_matrix;
	shadow_matrix.Translate(offset_pixel, offset_pixel);

	shadow_path.Transform(&shadow_matrix);

	Region path_region(&draw_path);
	Region shaow_region(&shadow_path);

	shaow_region.Exclude(&path_region);

	PathGradientBrush gradient_brush(&shadow_path);
	gradient_brush.SetCenterColor(Color(32, 32, 32, 0));

	Color colors[] = {Color(0, 0, 0, 0)};

	INT   count = 1;

	gradient_brush.SetSurroundColors(colors, &count);
	gradient_brush.SetFocusScales(0.75f, 0.75f);

	graphics.FillRegion(&gradient_brush, &shaow_region);
}

void QImage::resize(qsize_t width, qsize_t height)
{
	IMAGE temp_image = IMAGE(width, height);

	DWORD* source = GetImageBuffer(&save_image);
	DWORD* final_buffer = GetImageBuffer(&temp_image);

	float rate_height = float(get_height() / height);
	float rate_width = float(get_width() / width);

	int   x_scale;
	int   y_scale;

	DWORD* temp;

	for (int y_count = 0; y_count < height; ++y_count)
	{
		for (int x_count = 0; x_count < width; ++x_count)
		{
			y_scale = rate_height * y_count;
			x_scale = rate_width * x_count;

			temp = GetImageBuffer(&save_image);

			for (int count = 0; count < y_scale; ++count)
			{
				temp += get_width();
			}

			final_buffer[x_count] = temp[x_scale];
		}

		final_buffer += width;
		source += get_width();
	}

	save_image = temp_image;
}

QNamespaceEnd